package com.zf.shuashua.ui.home;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.mediamain.android.view.FoxWallView;
import com.mediamain.android.view.bean.MessageData;
import com.mediamain.android.view.interfaces.FoxListener;
import com.zf.shuashua.R;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.base.BaseFragment;
import com.zf.shuashua.manager.track.TrackConstants;
import com.zf.shuashua.manager.track.TrackManager;
import com.zf.shuashua.utils.Util;

import java.util.ArrayList;

@Deprecated
public class FirstFragment extends BaseFragment {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter adapter;
    private ViewPager vp;
    private SlidingTabLayout tabLayout;
    private final String[] mTitles = {"热点", "推荐"};
    private HomeFragment homeFragment;

    HotHomeFragment hotHomeFragment;

    private FoxWallView mOxWallView;

    public FirstFragment() {
    }

    public boolean isHomeTab() {
        if (tabLayout == null) {
            return true;
        }
        return tabLayout.getCurrentTab() == 1;
    }

    @Override
    protected void initView(View rootView) {
        homeFragment = new HomeFragment();
        hotHomeFragment = HotHomeFragment.newInstance();
        mFragments.add(hotHomeFragment);
        mFragments.add(homeFragment);
        vp = rootView.findViewById(R.id.vp);
        tabLayout = rootView.findViewById(R.id.tl);
        adapter = new MyPagerAdapter(getChildFragmentManager());
        vp.setAdapter(adapter);
        tabLayout.setViewPager(vp, mTitles);
        tabLayout.setCurrentTab(1);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    TrackManager.getInstance().reportAdvertAction(TrackConstants.PAGE_ID_NEW, TrackConstants.ACTION_HOME_HOT, TrackConstants.NOTE_HOME_HOT);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mOxWallView = (FoxWallView) rootView.findViewById(R.id.TMAw1);
        initFoxView();
    }

    public void GoHOt() {
        tabLayout.setCurrentTab(0);
    }

    public void GoHome() {
        tabLayout.setCurrentTab(1);
    }

    /**
     * 推啊悬浮按钮
     */
    private void initFoxView() {
        mOxWallView.setAdListener(new FoxListener() {
            @Override
            public void onReceiveAd() {
                Log.d("========", "onReceiveAd");
            }

            @Override
            public void onFailedToReceiveAd(int errorCode, String errorMsg) {
                Log.d("========", "onFailedToReceiveAd  Message=" + errorMsg);
            }

            @Override
            public void onLoadFailed() {
                Log.d("========", "onLoadFailed");
            }

            @Override
            public void onCloseClick() {
                Log.d("========", "onCloseClick");
            }

            @Override
            public void onAdClick() {
                Log.d("========", "onAdClick");
            }

            @Override
            public void onAdExposure() {
                Log.d("========", "onAdExposure");
            }

            @Override
            public void onAdActivityClose(String s) {
                Log.d("========", "onAdActivityClose" + s);
//                if (!FoxBaseCommonUtils.isEmpty(s)){
//                    ToastUtils.showShort(s);
//                }
            }

            @Override
            public void onAdMessage(MessageData messageData) {

            }
        });
//        if(mOxWallView != null){
//            String userId = Store.getInstance().getuserId();
//            if(TextUtils.isEmpty(userId)){
//                userId = Util.getDeviceId(ShuaApplication.getContext());
//            }
//            mOxWallView.loadAd(361361,userId);
//        }
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (mOxWallView != null) {
            mOxWallView.post(() -> {
                String userId = Store.getInstance().getuserId();
                if (TextUtils.isEmpty(userId)) {
                    userId = Util.getDeviceId(ShuaApplication.getContext());
                }
                mOxWallView.loadAd(361361, userId);
            });
        }
    }

    @Override
    protected void initViewModel() {

    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_first;

    }

    public void autoPraise() {
        homeFragment.autoPraise();
    }

    public void autoJumpNextVideo() {
        homeFragment.autoJumpNextVideo();
    }

    public void showShareDialog() {
        homeFragment.showShareDialog();
    }

    public void autoClickAd() {
        if (homeFragment != null) {
            homeFragment.autoClickAd();
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {

            return mFragments.get(position);
        }

    }

    @Override
    public void onDestroy() {
        if (mOxWallView != null) {
            mOxWallView.destroy();
        }
        super.onDestroy();
    }

}
